package com.lmx.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lmx.persistence.service.CallManageService;
import com.lmx.repoDomain.model.CallPageInfo;

@Controller
@RequestMapping("/backStand")
public class CallManageController {

	private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    CallManageService callManageService;
    
    @Autowired
    HttpServletRequest request;
    
    //获取通话记录列表
    @RequestMapping(value = "/getCallList", method = RequestMethod.GET)
    public String  getCallList(ModelMap model,CallPageInfo callPageInfo) {
    	
    	Map<String ,Object> values = new HashMap<String ,Object>();
    	
    	//验证当前页数
    	if(!String.valueOf(callPageInfo.getPage()).isEmpty()){
    		int pageNum = (callPageInfo.getPage()-1)*callPageInfo.getSize();
    		values.put("pageNum", pageNum);
    		values.put("size", callPageInfo.getSize());
    		model.put("page", callPageInfo.getPage());
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	String loginName = callPageInfo.getLoginName();//用户名
    	values.put("loginName", loginName);
    	model.put("loginName", loginName==null?loginName="":loginName);
    	
    	String taskName = callPageInfo.getTaskName();//任务名称
    	values.put("taskName", taskName);
    	model.put("taskName", taskName==null?taskName="":taskName);
    	
    	String startTime = callPageInfo.getStartTime();//开始时间
    	values.put("startTime", startTime);
    	model.put("startTime", startTime==null?startTime="":startTime);
    	
    	String enTime = callPageInfo.getEnTime();//截止时间
    	values.put("enTime", enTime);
    	model.put("enTime", enTime==null?enTime="":enTime);
    	
    	//获取通话记录列表
    	List<Map<String ,Object>> callList = callManageService.getCallList(values);
    	//获取通话记录列表总条数
    	int count = callManageService.getCallListCount(values);
    	
    	model.put("callList", callList);
    	model.put("count", count);
    	
        return "page/business/callManageList";
    }    
    
    
    //获取通话记录详情
    @RequestMapping(value = "/getCall", method = RequestMethod.GET)
    public String  getCall(ModelMap model,CallPageInfo callPageInfo) {
    	//验证callID
    	Long callID = callPageInfo.getCallID();
    	if(callID == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	Map<String ,Object> callMap = callManageService.getCall(callID);
    	
    	model.put("callMap", callMap);
    	model.put("callID", callID.toString());
    	String url = callPageInfo.getUrl();
    	model.put("url", url==null?url="":url);
    	HttpSession session = request.getSession(); 
    	session.setAttribute("url", url==null?url="":url); 
    	session.setMaxInactiveInterval(120*60);//设置失效时长为2小时
    	
        return "page/business/callInfo";
    }
}
